package com.goldplusgold.tradedelay.app.trade.adapter;

import android.content.Context;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.goldplusgold.support.lib.base.adapter.AdapterParentBase;
import com.goldplusgold.support.lib.widget.horizontallistview.SyncHorizontalScrollView;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.trade.pojo.TransactionRecordVM;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by admin on 2017/5/18.
 */

public class AdapterTrade extends AdapterParentBase<TransactionRecordVM> implements SyncHorizontalScrollView.OnScrollListenter {
    private ArrayList<SyncHorizontalScrollView> mScrollViewList = new ArrayList<>();

    public AdapterTrade(Context ctx) {
        super(ctx);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.item_position, parent, false);
            holder = new ViewHolder(convertView);
            for (int i = 0; i < 5; i++) {
                View childView = getLayoutInflater().inflate(R.layout.item_horizontal_position, holder.mListViewPosition, false);
                holder.mListViewPosition.addView(childView);
            }
            holder.mListViewPosition.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
            mScrollViewList.add(holder.mSyncHorizontalScrollView);
            holder.mSyncHorizontalScrollView.setOnScrollListenter(this);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        TransactionRecordVM content = getItem(position);
        if (content != null) {
            // 合约代码
            holder.mTextViewTitlePosition.setText(content.getProdCode());
            holder.mTextViewTitlePosition.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
            int count = holder.mListViewPosition.getChildCount();
            for (int i = 0; i < count; i++) {
                View childView = holder.mListViewPosition.getChildAt(i);
                TextView item = (TextView) childView.findViewById(R.id.id_text_view_horizontal);
                item.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
                switch (i) {
                    case 0:
                        //类型
                        item.setText(content.getType());
                        break;
                    case 1:
                        //成交价
                        item.setText(String.valueOf(content.getMatchPrice()));
                        break;
                    case 2:
                        //  成交手数
                        item.setText(String.valueOf(content.getMatchAmount()));
                        break;
                    case 3:
                        //成交时间
                        item.setText(content.getMatchTime());
                        break;
                    case 4:
                        // 备注
                        item.setText(content.getNote());
                        break;
                }
            }

        }
        return convertView;
    }


    @Override
    public void onScroll(int x, int y) {
        for (SyncHorizontalScrollView scrollview :
                mScrollViewList) {
            scrollview.setScrollX(x);

        }
    }


    public static class ViewHolder {
        @BindView(R.id.id_text_view_title_position)
        TextView mTextViewTitlePosition;
        @BindView(R.id.id_list_view_position)
        LinearLayout mListViewPosition;
        @BindView(R.id.content_horsv)
        SyncHorizontalScrollView mSyncHorizontalScrollView;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


    public void addHead(SyncHorizontalScrollView view) {
        this.mScrollViewList.add(view);
    }
}
